from utils.excel_util import ExcelUtil
from model.sysOperLogModel import SysOperLog
from schema.sysOperLogSchema import SysOperLogCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysOperLogExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "title", "title": "模块标题", "required": False},

        {"field": "business_type", "title": "业务类型（0其它 1新增 2修改 3删除）", "required": False},

        {"field": "method", "title": "方法名称", "required": False},

        {"field": "request_method", "title": "请求方式", "required": False},

        {"field": "operator_type", "title": "操作类别（0其它 1后台用户 2手机端用户）", "required": False},

        {"field": "oper_name", "title": "操作人员", "required": False},

        {"field": "dept_name", "title": "部门名称", "required": False},

        {"field": "oper_url", "title": "请求URL", "required": False},

        {"field": "oper_ip", "title": "主机地址", "required": False},

        {"field": "oper_location", "title": "操作地点", "required": False},

        {"field": "oper_param", "title": "请求参数", "required": False},

        {"field": "json_result", "title": "返回参数", "required": False},

        {"field": "oper_status", "title": "操作状态（0正常 1异常）", "required": False},

        {"field": "status", "title": "状态（0正常 2删除）", "required": False},

        {"field": "error_msg", "title": "错误消息", "required": False},

        {"field": "oper_time", "title": "操作时间", "required": False},

        {"field": "cost_time", "title": "消耗时间", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

        {"field": "remark", "title": "备注", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "title", "title": "模块标题"},

            {"field": "business_type", "title": "业务类型（0其它 1新增 2修改 3删除）"},

            {"field": "method", "title": "方法名称"},

            {"field": "request_method", "title": "请求方式"},

            {"field": "operator_type", "title": "操作类别（0其它 1后台用户 2手机端用户）"},

            {"field": "oper_name", "title": "操作人员"},

            {"field": "dept_name", "title": "部门名称"},

            {"field": "oper_url", "title": "请求URL"},

            {"field": "oper_ip", "title": "主机地址"},

            {"field": "oper_location", "title": "操作地点"},

            {"field": "oper_param", "title": "请求参数"},

            {"field": "json_result", "title": "返回参数"},

            {"field": "oper_status", "title": "操作状态（0正常 1异常）"},

            {"field": "status", "title": "状态（0正常 2删除）"},

            {"field": "error_msg", "title": "错误消息"},

            {"field": "oper_time", "title": "操作时间"},

            {"field": "cost_time", "title": "消耗时间"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

            {"field": "remark", "title": "备注"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "title": "example",

            "business_type": 1,

            "method": "example",

            "request_method": "example",

            "operator_type": 1,

            "oper_name": "example",

            "dept_name": "example",

            "oper_url": "example",

            "oper_ip": "example",

            "oper_location": "example",

            "oper_param": "example",

            "json_result": "example",

            "oper_status": 1,

            "status": "A",

            "error_msg": "example",

            "oper_time": datetime.now(),

            "cost_time": 1,

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

            "remark": "example",

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="操作日志记录导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="操作日志记录数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysOperLogCreate
        )